package com.cyzy.server.service.impl;

import com.cyzy.common.exception.SelectNullException;
import com.cyzy.pojo.dto.HuntCardDTO;
import com.cyzy.pojo.dto.HuntCardUpdateDTO;
import com.cyzy.pojo.entity.HuntCard;
import com.cyzy.pojo.entity.HuntTag;
import com.cyzy.pojo.vo.HuntCardVO;
import com.cyzy.server.dao.HuntCardDao;
import com.cyzy.server.dao.HuntCardTagsDao;
import com.cyzy.server.service.HuntCardService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class HuntCardServiceImpl implements HuntCardService {

    @Autowired
    private HuntCardDao huntCardDao;
    @Autowired
    private HuntCardTagsDao huntCardTagsDao;

    /**
     * 保存求职帖子
     *
     * @param huntCardDTO
     * @return
     */
    @Override
    @Transactional
    public HuntCard save(HuntCardDTO huntCardDTO) {
        HuntCard huntCard = new HuntCard();
        //属性拷贝
        BeanUtils.copyProperties(huntCardDTO, huntCard);

        //默认设置为展示状态
        huntCard.setDisplayed(1);

        //设置发布时间
        huntCard.setPostTime(LocalDateTime.now());

        //除了tags以外的值插入表中
        huntCardDao.insert(huntCard);
        //获得insert语句产生的主键值
        Integer huntCardId = huntCard.getId();

        //处理招募帖子的tags
        List<HuntTag> tags = huntCardDTO.getTags();

        huntCard.setTags(tags);

        if (tags != null && tags.size() > 0) {
            tags.forEach(huntTag -> {
                huntTag.setHuntCardId(huntCardId);
            });
            //向招募帖子的标签表插入数据
            huntCardTagsDao.insertBatch(tags);
        }

        return huntCard;
    }

    /**
     * 返回求职帖子的首页列表
     *
     * @return
     */
    @Override
    public List<HuntCardVO> selectHuntCardList() {
        List<HuntCard> huntCardList = huntCardDao.selectHuntCard();

        if(huntCardList == null){
            throw new SelectNullException("拉取数据库信息为空");
        }

        List<HuntCardVO> huntCardVOList = new ArrayList<>();

        for (HuntCard huntCard : huntCardList) {
            //将每个huntCard的元素拷贝到VO里返回
            HuntCardVO huntCardVO = new HuntCardVO();
            BeanUtils.copyProperties(huntCard, huntCardVO);
            //获取求职帖子的id
            Integer huntCardId = huntCard.getId();

            huntCardVO.setHuntCardId(huntCardId);
            //从tags表中查出求职贴的标签，然后返回
            List<HuntTag> tags = huntCardTagsDao.selectHuntTagByHuntId(huntCardId);

            huntCardVO.setTags(tags);
            huntCardVOList.add(huntCardVO);
        }

        return huntCardVOList;

    }

    /**
     * 返回用户本人发布的求职帖子列表
     *
     * @return
     */

    @Override
    public List<HuntCardVO> selectHuntCardListByUserId(Integer id) {

        List<HuntCard> huntCardList = huntCardDao.selectHuntCardByPublisher(id);
        List<HuntCardVO> huntCardVOList = new ArrayList<>();

        for (HuntCard huntCard : huntCardList) {
            //将每个huntCard的元素拷贝到VO里返回
            HuntCardVO huntCardVO = new HuntCardVO();
            BeanUtils.copyProperties(huntCard, huntCardVO);
            //获取求职帖子的id
            Integer huntCardId = huntCard.getId();

            huntCardVO.setHuntCardId(huntCardId);
            //从tags表中查出求职贴的标签，然后返回
            List<HuntTag> tags = huntCardTagsDao.selectHuntTagByHuntId(huntCardId);

            huntCardVO.setTags(tags);
            huntCardVOList.add(huntCardVO);
        }

        return huntCardVOList;

    }

    /**
     * 根据id返回求职帖子详情页
     *
     * @param id
     * @return
     */
    @Override
    public HuntCard selectHuntCardById(Integer id) {
        HuntCard huntCard = huntCardDao.selectHuntCardById(id);
        if(huntCard == null){
            throw new SelectNullException("拉取数据库信息为空");
        }
        List<HuntTag> huntTags = huntCardTagsDao.selectHuntTagByHuntId(id);
        huntCard.setTags(huntTags);
        return huntCard;
    }

    @Override
    @Transactional
    public HuntCard updateHuntCardById(Integer id, HuntCardUpdateDTO huntCardUpdateDTO) {
        HuntCard huntCard = new HuntCard();
        huntCard.setId(id);
        //将dto的数据拷贝到实体类里
        BeanUtils.copyProperties(huntCardUpdateDTO, huntCard);

        huntCardDao.update(huntCard);


        //删除对应id下的所有标签
        huntCardTagsDao.deleteByHuntCardId(id);

        //获取tag
        List<HuntTag> tags = huntCardUpdateDTO.getTags();
        if (tags != null && tags.size() > 0) {
            tags.forEach(huntTag -> {
                huntTag.setHuntCardId(id);
            });
            //向招募标签表插入n条数据
            huntCardTagsDao.insertBatch(tags);

        }
        HuntCard huntCardAfterUpdate = huntCardDao.selectHuntCardById(id);

        if(huntCardAfterUpdate == null){
            throw new SelectNullException("拉取数据库信息为空");
        }
        huntCardAfterUpdate.setTags(tags);
        return huntCardAfterUpdate;
    }
}
